﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

namespace ShpMergerGUI
{
    public partial class frmMainWindow : Form
    {
        private ConsoleShower consoleShow;
        private FileMeger fileMeger;
        private bool isStart;
        private bool isPuase;
        private bool isStop;
        private delegate void UpdateProgress();

        public frmMainWindow()
        {
            consoleShow = new ConsoleShower();
            fileMeger = new FileMeger();
            fileMeger.Merging += new EventHandler<EventArgs>(fileMeger_Merging);
            fileMeger.Merged += new EventHandler<EventArgs>(fileMeger_Merged);
            isStart = false;
            isPuase = false;
            isStop = true;
            InitializeComponent();
            lblProgress.Text = OutputString.MergeProgress(0);
            lblInfo.Text = OutputString.WaitMerge;
        }

        private void ChooseSrcFiles()
        {
            OpenFileDialog srcFilesChooser = new OpenFileDialog();
            srcFilesChooser.Multiselect = true;
            srcFilesChooser.FilterIndex = 0;
            srcFilesChooser.Title = "选择shp文件";
            srcFilesChooser.Filter = "地图文件.shp|*.shp";
            if (srcFilesChooser.ShowDialog() == DialogResult.OK)
            {
                AddSrcFiles(srcFilesChooser.FileNames);
            }
        }

        private void ChooseDestFile()
        {
            SaveFileDialog destFileChooser = new SaveFileDialog();
            destFileChooser.FilterIndex = 1;
            destFileChooser.Title = "保存为";
            destFileChooser.DefaultExt = "shp";
            destFileChooser.FileName = "DsetinationFile.shp";
            destFileChooser.Filter = "地图文件.shp|*.shp|所有文件*.*|*.*";
            if (DialogResult.OK == destFileChooser.ShowDialog())
            {
                cmbDestFile.Text =  destFileChooser.FileName;
            }
        }

        private void AddSrcFile(string srcFile)
        {
            bool canAdd = true;
            foreach (ListViewItem item in lvwSrcFiles.Items)
            {
                if (item.Text == srcFile)
                {
                    canAdd = false;
                }
            }
            if (canAdd)
            {
                lvwSrcFiles.Items.Add(new ListViewItem(srcFile));
            }
        }

        private void AddSrcFiles(string[] srcFiles)
        {
            foreach (string file in srcFiles)
            {
                AddSrcFile(file);
            }
            lblSrcFileNumber.Text = OutputString.SrcFileNumber(lvwSrcFiles.Items.Count);
        }

        private void AddSrcFiles(List<string> srcFiles)
        {
            foreach (string file in srcFiles)
            {
                AddSrcFile(file);
            }
            lblSrcFileNumber.Text = OutputString.SrcFileNumber(lvwSrcFiles.Items.Count);
        }

        private void RemoveSelectedSrcFiles()
        {
            List<ListViewItem> removeItems = new List<ListViewItem>();
            foreach (ListViewItem item in lvwSrcFiles.Items)
            {
                if (item.Selected)
                {
                    lvwSrcFiles.Items.Remove(item);
                }
            }
            lblSrcFileNumber.Text = OutputString.SrcFileNumber(lvwSrcFiles.Items.Count);
        }

        private void RemoveSrcFiles()
        {
            lvwSrcFiles.Items.Clear();
            lblSrcFileNumber.Text = OutputString.SrcFileNumber(lvwSrcFiles.Items.Count);
        }

        private void StartMerge()
        {
            if (!isStart && !isPuase && isStop)
            {
                //new merge
                isStart = true;
                isPuase = false;
                isStop = false;

                if (Path.GetExtension(cmbDestFile.Text).ToLower() != ".shp")
                {
                    tipRemind.Show(OutputString.WrongDestFile, cmbDestFile, 2345);
                }
                else if (!Directory.Exists(Path.GetDirectoryName(cmbDestFile.Text)))
                {
                    tipRemind.Show(OutputString.WrongDestDirectory, cmbDestFile, 2345);
                }
                else if (lvwSrcFiles.Items.Count < 2)
                {
                    tipRemind.Show(OutputString.LackSrcFiles, lvwSrcFiles, 2345);
                }
                else
                {
                    List<string> srcFiles = new List<string>();
                    foreach(ListViewItem item in lvwSrcFiles.Items)
                    {
                        srcFiles.Add(item.Text);
                    }
                    prgMerge.Value = 0;
                    prgMerge.Maximum = srcFiles.Count;
                    lblProgress.Text = OutputString.MergeProgress(0);
                    lblInfo.Text = OutputString.StartMerge;
                    fileMeger.StartMerge(srcFiles, cmbDestFile.Text);
                }
            }
            else if (!isStart && isPuase && !isStop)
            {
                //go on
                isStart = true;
                isPuase = false;
                isStop = false;
                fileMeger.ReStartMerge();
            }
        }

        private void PauseMerge()
        {
            if (isStart && !isPuase && !isStop)
            {
                //pause
                isStart = false;
                isPuase = true;
                isStop = false;
                fileMeger.PuaseMerge();
            }
        }

        private void StopMerge()
        {
            if (isStart && !isPuase && !isStop
                    || !isStart && isPuase && !isStop)
            {
                //stop
                isStart = false;
                isPuase = false;
                isStop = true;
                fileMeger.StopMerge();
            }
        }

        private void InprgMerge_Merging()
        {
            ++prgMerge.Value;
        }

        private void InlblInfo_Merging()
        {
            lblInfo.Text = OutputString.MergInfo(fileMeger.GetMergingFile());
        }

        private void InlblProgress_Merging()
        {
             lblProgress.Text = OutputString.MergeProgress(fileMeger.GetMergePercent());
        }

        private void InprgMerge_Merged()
        {
            prgMerge.Value = prgMerge.Maximum;
        }

        private void InlblInfo_Merged()
        {
            lblInfo.Text = OutputString.EndMerge;
        }

        private void InlblProgress_Merged()
        {
            lblProgress.Text = OutputString.MergeProgress(100);
        }

        private void fileMeger_Merging(object sender, EventArgs e)
        {
            prgMerge.Invoke(new UpdateProgress(InprgMerge_Merging));
            lblInfo.Invoke(new UpdateProgress(InlblInfo_Merging));
            lblProgress.Invoke(new UpdateProgress(InlblProgress_Merging));
        }

        private void fileMeger_Merged(object sender, EventArgs e)
        {
            prgMerge.Invoke(new UpdateProgress(InprgMerge_Merged));
            lblInfo.Invoke(new UpdateProgress(InlblInfo_Merged));
            lblProgress.Invoke(new UpdateProgress(InlblProgress_Merged));
            isStart = false;
            isPuase = false;
            isStop = true;
        }

        private void cmstsmiIndex0_Click(object sender, EventArgs e)
        {
            ChooseSrcFiles();
        }

        private void cmstsmiIndex1_Click(object sender, EventArgs e)
        {
            RemoveSelectedSrcFiles();
        }

        private void cmstsmiIndex2_Click(object sender, EventArgs e)
        {
            RemoveSrcFiles();
        }

        private void cmstsmiIndex3_Click(object sender, EventArgs e)
        {
            StartMerge();
        }

        private void cmstsmiIndex4_Click(object sender, EventArgs e)
        {
            PauseMerge();
        }

        private void cmstsmiIndex5_Click(object sender, EventArgs e)
        {
            StopMerge();
        }

        private void mnstsmiHeader0Index0_Click(object sender, EventArgs e)
        {
            ChooseSrcFiles();
        }

        private void mnstsmiHeader0Index1_Click(object sender, EventArgs e)
        {
            RemoveSelectedSrcFiles();
        }

        private void mnstsmiHeader0Index2_Click(object sender, EventArgs e)
        {
            RemoveSrcFiles();
        }

        private void mnstsmiHeader0Index3_Click(object sender, EventArgs e)
        {
            ChooseDestFile();
        }

        private void mnstsmiHeader0Index4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnstsmiHeader1Index0_Click(object sender, EventArgs e)
        {
            StartMerge();
        }

        private void mnstsmiHeader1Index1_Click(object sender, EventArgs e)
        {
            PauseMerge();
        }

        private void mnstsmiHeader1Index2_Click(object sender, EventArgs e)
        {
            StopMerge();
        }

        private void mnstsmiHeader2Index0_Click(object sender, EventArgs e)
        {
            consoleShow.RunConsole();
        }

        private void mnstsmiHeader3Index0_Click(object sender, EventArgs e)
        {
            MessageBox.Show(OutputString.Help, "帮助", MessageBoxButtons.OK, 
                MessageBoxIcon.Question);
        }

        private void mnstsmiHeader3Index1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(OutputString.Version, "版本信息",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void lvwSrcFiles_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            ChooseSrcFiles();
        }

        private void lvwSrcFiles_DragDrop(object sender, DragEventArgs e)
        {
            Array dropObjects = (Array)e.Data.GetData(DataFormats.FileDrop);
            List<string> srcFiles = new List<string>();
            for (int i = 0; i < dropObjects.Length; i++)
            {
                srcFiles.Add(dropObjects.GetValue(i).ToString());
            }
            AddSrcFiles(srcFiles);
        }

        private void lvwSrcFiles_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }

        private void btnDestFile_Click(object sender, EventArgs e)
        {
            ChooseDestFile();
        }
     
        private void BtnStart_Click(object sender, EventArgs e)
        {
            StartMerge();
        }

        private void BtnPause_Click(object sender, EventArgs e)
        {
            PauseMerge();
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            StopMerge();
        }

        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMainWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            fileMeger.StopMerge();
        }
    }   
}
